package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StoreRepositoryImpl implements StoreRepository {
    private final StoreMapper storeMapper;
    @Autowired
    public StoreRepositoryImpl(StoreMapper storeMapper) {
        this.storeMapper = storeMapper;
    }

    public List<StorePO> selectByBusinessId(Integer businessId) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        return storeMapper.selectList(queryWrapper);
    }

    public PageResult<StorePO> queryStore(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        String storeName = query.getStoreName();
        if(storeName != null && !storeName.trim().isEmpty()) {
            queryWrapper.eq("store_name", query.getStoreName());
        }
        String phone = query.getStoreHeadPhone();
        if(phone!= null &&!phone.trim().isEmpty()) {
            queryWrapper.eq("store_head_phone", query.getStoreHeadPhone());
        }
        queryWrapper.eq(query.getProvinceId() != null,"province_id", query.getProvinceId());
        queryWrapper.eq(query.getCityId() != null,"city_id", query.getCityId());
        queryWrapper.eq(query.getAreaId() != null,"area_id", query.getAreaId());
        queryWrapper.eq(query.getStoreStatus() != null,"store_status",query.getStoreStatus());
        queryWrapper.eq(query.getBusinessId() != null,"business_id",query.getBusinessId());
        queryWrapper.gt(query.getStartingTime() != null, "update_time", query.getStartingTime());
        queryWrapper.lt(query.getEndTime() != null, "update_time", query.getEndTime());
        List<StorePO> poList = storeMapper.selectList(queryWrapper);
        Long total = storeMapper.selectCount(queryWrapper);
        PageResult<StorePO> pr = new PageResult<>();
        pr.setTotal(total);
        pr.setObjects(poList);
        return pr;
    }

    public void save(StorePO po) {
        storeMapper.insert(po);
    }
}
